/*
 * 
 * 
 * 
 */
package com.hboxs.asl.service.impl;

import com.hboxs.asl.Principal;
import com.hboxs.asl.dao.MerchantDao;
import com.hboxs.asl.entity.Merchant;
import com.hboxs.asl.service.MerchantService;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Service - 商家
 */
@Service("merchantServiceImpl")
public class MerchantServiceImpl extends BaseServiceImpl<Merchant, Long> implements MerchantService {

    @Resource(name = "merchantDaoImpl")
    private MerchantDao merchantDao;

    @Resource(name = "merchantDaoImpl")
    public void setBaseDao(MerchantDao merchantDao) {
        super.setBaseDao(merchantDao);
    }


    @Override
    public Merchant findByUsername(String username) {
        return merchantDao.findByUsername(username);
    }

    @Override
    public Merchant getCurrent() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        if (requestAttributes != null) {
            HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
            Principal principal = (Principal) request.getSession().getAttribute(Merchant.SESSION_KEY);
            if (principal != null) {
                return merchantDao.find(principal.getId());
            }
        }
        return null;
    }

    @Override
    public Merchant findByCode(String code) {
        return merchantDao.findByCode(code);
    }
}
